namespace CodeCamp.EFDAL.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<CodeCamp.EFDAL.CodeCampDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        /*
        -- KEEP THIS COMMENT - used to sync compare with schema SQL file
        -- Quote the commented out drop statements so they will compare when comparing this with Configuration.cs
         */
        private static class CreationSql
        {

            internal const string UQ__000 =
@"
--'Drop Index cc_000'
";

            internal const string UQ__BannedTags_Name =
@"
--'Drop Index cc_000'
--'Drop Index cc_BannedTags.UQ__BannedTags_Name'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_BannedTags', @IndexName = 'UQ__BannedTags_Name';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__BannedTags_Name] on dbo.cc_BannedTags( Name  );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Buildings_Name4Event =
@"
--'Drop Index cc_Buildings.UQ__Buildings_Name4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Buildings', @IndexName = 'UQ__Buildings_Name4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Buildings_Name4Event] on dbo.cc_Buildings( EventId, Name  );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__CommunitySponsors_Name4Event =
@"
--'Drop Index cc_CommunitySponsors.UQ__CommunitySponsors_Name4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_CommunitySponsors', @IndexName = 'UQ__CommunitySponsors_Name4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__CommunitySponsors_Name4Event] on dbo.cc_CommunitySponsors( EventId, Name  );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Events_DisplayName =
@"
--'Drop Index cc_Events.UQ__Events_DisplayName'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Events', @IndexName = 'UQ__Events_DisplayName';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Events_DisplayName] on dbo.cc_Events( DisplayName );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Events_ShortName =
@"
--'Drop Index cc_Events.UQ__Events_ShortName'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Events', @IndexName = 'UQ__Events_ShortName';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Events_ShortName] on dbo.cc_Events( ShortName );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__IndividualContributors_Names4Event =
@"
--'Drop Index cc_IndividualContributors.UQ__IndividualContributors_Names4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_IndividualContributors', @IndexName = 'UQ__IndividualContributors_Names4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__IndividualContributors_Names4Event] on dbo.cc_IndividualContributors( EventId, FirstName, LastName  );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";
#if false
            internal const string UQ__Presenters_EmailAddress4Event =
@"
--'Drop Index cc_Presenters.UQ__Presenters_EmailAddress4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Presenters', @IndexName = 'UQ__Presenters_EmailAddress4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Presenters_EmailAddress4Event] on dbo.cc_Presenters( EventId, EmailAddress );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";
#endif

#if false
            internal const string UQ__Presenters_UniqueUserIdentifier =
@"
--'Drop Index cc_Presenters.UQ__Presenters_UniqueUserIdentifier'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Presenters', @IndexName = 'UQ__Presenters_UniqueUserIdentifier';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Presenters_UniqueUserIdentifier] on dbo.cc_Presenters( EventId, UniqueUserIdentifier );
		Print 'Created Index ' + @IndexName;
	End;
END;
";
#endif

            internal const string UQ__Rooms_Name4Building =
@"
--'Drop Index cc_Rooms.UQ__Rooms_Name4Building'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Rooms', @IndexName = 'UQ__Rooms_Name4Building';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Rooms_Name4Building] on dbo.cc_Rooms( BuildingId, Name );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Sessions_Title4Event =
@"
--'Drop Index cc_Sessions.UQ__Sessions_Title4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Sessions', @IndexName = 'UQ__Sessions_Title4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Sessions_Title4Event] on dbo.cc_Sessions( EventId, Title  );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__SessionTags_Tags4Session =
@"
--'Drop Index cc_SessionTags.UQ__SessionTags_Tags4Session'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_SessionTags', @IndexName = 'UQ__SessionTags_Tags4Session';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__SessionTags_Tags4Session] on dbo.cc_SessionTags( SessionId, TagId  );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

#if false
            internal const string UQ__ShirtSizes_Size =
@"
--'Drop Index cc_ShirtSizes.UQ__ShirtSizes_Size'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_ShirtSizes', @IndexName = 'UQ__ShirtSizes_Size';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__ShirtSizes_Size] on dbo.cc_ShirtSizes( Size );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";
#endif

            internal const string UQ__SiteUsers_IdentityProviderAndIdentityClaim =
@"
--'Drop Index UQ__SiteUsers_IdentityProviderAndIdentityClaim on cc_SiteUsers'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_SiteUsers', @IndexName = 'UQ__SiteUsers_IdentityProviderAndIdentityClaim';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__SiteUsers_IdentityProviderAndIdentityClaim] on dbo.cc_SiteUsers( UserIdentityProvider, UserIdentityClaim );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Sponsors_Name4Event =
@"
--'Drop Index cc_Sponsors.UQ__Sponsors_Name4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Sponsors', @IndexName = 'UQ__Sponsors_Name4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Sponsors_Name4Event] on dbo.cc_Sponsors( EventId, Name );
		--Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__Tags_Name4Event =
@"
--'Drop Index cc_Tags.UQ__Tags_Name4Event'
Declare @SchemaName nvarchar(50), @TableName nvarchar(100), @SchemaTableName nvarchar(151), @SqlCmd nvarchar(512), @IndexName nvarchar(100);
Select @SchemaName = 'dbo', @TableName = 'cc_Tags', @IndexName = 'UQ__Tags_Name4Event';
Set @SchemaTableName = @SchemaName + '.' + @TableName;
If OBJECT_ID(@SchemaTableName,'U') is null Begin
	Print 'Table ' + IsNull(@SchemaTableName,'???') + ' does not exist.';
End Else Begin
	IF EXISTS (Select name from sys.indexes si Where si.name = @IndexName and si.object_id = OBJECT_ID(@SchemaTableName,'U')) Begin
		Print 'Index ' + @IndexName + ' exists.';
	End Else Begin
		Create unique index [UQ__Tags_Name4Event] on dbo.cc_Tags( EventId, Name );
		Print 'Created Index ' + @IndexName;
	End;
END;
";

            internal const string UQ__zzz =
@"
--'Drop Index cc_zzz'
";

        }

        protected override void Seed(CodeCamp.EFDAL.CodeCampDbContext context)
        {
            if (context != null)
            {
                context.Database.ExecuteSqlCommand(CreationSql.UQ__BannedTags_Name);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Buildings_Name4Event);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__CommunitySponsors_Name4Event);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Events_DisplayName);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Events_ShortName);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__IndividualContributors_Names4Event);
                //context.Database.ExecuteSqlCommand(CreationSql.UQ__Presenters_EmailAddress4Event);
                //context.Database.ExecuteSqlCommand(CreationSql.UQ__Presenters_UniqueUserIdentifier);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Rooms_Name4Building);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Sessions_Title4Event);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__SessionTags_Tags4Session);
                //context.Database.ExecuteSqlCommand(CreationSql.UQ__ShirtSizes_Size);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__SiteUsers_IdentityProviderAndIdentityClaim);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Sponsors_Name4Event);
                context.Database.ExecuteSqlCommand(CreationSql.UQ__Tags_Name4Event);
            }
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}
